package com.moxiao.yfjh.module.blind.controller.admin.moment;

import static com.moxiao.yfjh.framework.common.pojo.CommonResult.success;
import static com.moxiao.yfjh.framework.operatelog.core.enums.OperateTypeEnum.EXPORT;

import com.moxiao.yfjh.framework.common.pojo.CommonResult;
import com.moxiao.yfjh.framework.common.pojo.PageResult;
import com.moxiao.yfjh.framework.excel.core.util.ExcelUtils;
import com.moxiao.yfjh.framework.operatelog.core.annotations.OperateLog;
import com.moxiao.yfjh.module.blind.controller.admin.moment.vo.BlindMemberMomentCreateReqVO;
import com.moxiao.yfjh.module.blind.controller.admin.moment.vo.BlindMemberMomentExcelVO;
import com.moxiao.yfjh.module.blind.controller.admin.moment.vo.BlindMemberMomentExportReqVO;
import com.moxiao.yfjh.module.blind.controller.admin.moment.vo.BlindMemberMomentPageReqVO;
import com.moxiao.yfjh.module.blind.controller.admin.moment.vo.BlindMemberMomentRespVO;
import com.moxiao.yfjh.module.blind.controller.admin.moment.vo.BlindMemberMomentUpdateReqVO;
import com.moxiao.yfjh.module.blind.controller.admin.moment.vo.BlindMementStatisticsVo;
import com.moxiao.yfjh.module.blind.convert.moment.BlindMemberMomentConvert;
import com.moxiao.yfjh.module.blind.dal.dataobject.member.BlindMemberUserDO;
import com.moxiao.yfjh.module.blind.dal.dataobject.moment.BlindMemberMomentDO;
import com.moxiao.yfjh.module.blind.service.member.BlindMemberUserService;
import com.moxiao.yfjh.module.blind.service.moment.BlindMemberMomentService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import java.io.IOException;
import java.util.Collection;
import java.util.List;
import java.util.Objects;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@Tag(name = "管理后台 - 相亲会员动态")
@RestController
@RequestMapping("/blind/member-moment")
@Validated
public class BlindMemberMomentController {

    @Resource
    private BlindMemberMomentService memberMomentService;
    @Resource
    private BlindMemberUserService blindMemberUserService;


    @PostMapping("/create")
    @Operation(summary = "创建相亲会员动态")
    @PreAuthorize("@ss.hasPermission('blind:member-moment:create')")
    public CommonResult<Long> createMemberMoment(@Valid @RequestBody BlindMemberMomentCreateReqVO createReqVO) {
        return success(memberMomentService.createMemberMoment(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新相亲会员动态")
    @PreAuthorize("@ss.hasPermission('blind:member-moment:update')")
    public CommonResult<Boolean> updateMemberMoment(@Valid @RequestBody BlindMemberMomentUpdateReqVO updateReqVO) {
        memberMomentService.updateMemberMoment(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除相亲会员动态")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('blind:member-moment:delete')")
    public CommonResult<Boolean> deleteMemberMoment(@RequestParam("id") Long id) {
        memberMomentService.deleteMemberMoment(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得相亲会员动态")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('blind:member-moment:query')")
    public CommonResult<BlindMemberMomentRespVO> getMemberMoment(@RequestParam("id") Long id) {
        BlindMemberMomentDO memberMoment = memberMomentService.getMemberMoment(id);
        return success(BlindMemberMomentConvert.INSTANCE.convert(memberMoment));
    }

    @GetMapping("/list")
    @Operation(summary = "获得相亲会员动态列表")
    @Parameter(name = "ids", description = "编号列表", required = true, example = "1024,2048")
    @PreAuthorize("@ss.hasPermission('blind:member-moment:query')")
    public CommonResult<List<BlindMemberMomentRespVO>> getMemberMomentList(@RequestParam("ids") Collection<Long> ids) {
        List<BlindMemberMomentDO> list = memberMomentService.getMemberMomentList(ids);
        return success(BlindMemberMomentConvert.INSTANCE.convertList(list));
    }

    @GetMapping("/page")
    @Operation(summary = "获得相亲会员动态分页")
    @PreAuthorize("@ss.hasPermission('blind:member-moment:query')")
    public CommonResult<PageResult<BlindMemberMomentRespVO>> getMemberMomentPage(@Valid BlindMemberMomentPageReqVO pageVO) {
        PageResult<BlindMemberMomentDO> pageResult = memberMomentService.getMemberMomentPage(pageVO);
        return success(BlindMemberMomentConvert.INSTANCE.convertPage(pageResult));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出相亲会员动态 Excel")
    @PreAuthorize("@ss.hasPermission('blind:member-moment:export')")
    @OperateLog(type = EXPORT)
    public void exportMemberMomentExcel(@Valid BlindMemberMomentExportReqVO exportReqVO,
              HttpServletResponse response) throws IOException {
        List<BlindMemberMomentDO> list = memberMomentService.getMemberMomentList(exportReqVO);
        // 导出 Excel
        List<BlindMemberMomentExcelVO> datas = BlindMemberMomentConvert.INSTANCE.convertList02(list);
        ExcelUtils.write(response, "相亲会员动态.xls", "数据", BlindMemberMomentExcelVO.class, datas);
    }

    @GetMapping("/getMomentStatistics")
    @Operation(summary = "获得会员动态统计数据")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    public CommonResult<BlindMementStatisticsVo> getMomentStatistics(@RequestParam("id") Long id) {
        BlindMementStatisticsVo memberMomentStatistics = memberMomentService.getMemberMomentStatistics(id);
        BlindMemberUserDO memberUser = blindMemberUserService.getMemberUser(id);
        if (Objects.nonNull(memberUser)){
            memberMomentStatistics.setAvatar(memberUser.getAvatar());
        }
        return success(memberMomentStatistics);
    }


}
